<?php 
App::uses('AppController', 'Controller');

class ShopCategoriesController extends AppController {
	
	var $components = array('Link','Files','Images','Strings');
	var $user;
	public function beforeFilter(){
		$this->user = parent::checkMemberLogin();
		$this->set("user", $this->user);
	}
	/**
	 * @author: QuocTran
	 * them danh mục cho shop
	 * param : id => shop id
	 */		
	public function index() {
		$this->layout = 'layoutNoMenuLeft';
		
		$this->loadModel('RootCategory');
		$this->loadModel('ShopCategory');
		$this->loadModel('Shop');
		
		$this->set('rootCategories',$this -> RootCategory->findAllData());
		$this->set('title_for_layout','Đăng ký danh mục');
		
		
		
		
		// kiem tra shop này có phải do user dang nhập hiện tại tạo ra hay ko?
		$conditions = array("Shop.created_by" => $this->user['id'], "Shop.deleted" =>0);
		$shop = $this->Shop->find('first', array('conditions' => $conditions));
		
		if(empty($shop)){
			$this->redirect("/quan-ly-user");
		}
		$shopId = $shop["Shop"]['id'];
		
		$this->set("shopId",$shop["Shop"]['id']);
		
		// nếu ko có shop thì ko dc vào trang này
		//$shop = $this->Shop->findShopById($shopId);
		
		
		//nếu đã có shop categories thì chỉ dc vào trang edit
		$shopCat = $this->ShopCategory->findShopCategoryByShopId($shop["Shop"]['id']);
		if(count($shopCat) > 0){
			$this->redirect("/quan-ly-user");
		}
		
		if ($this -> request -> is('post')) {
			
			$data = $this -> request ->data;
			
			$new = array();
			
			foreach ($data as $item){
				if(!empty($item['shopCat'])>0){
					foreach ($item['shopCat'] as $i){
						$shopCat['category_id'] = $i;
						$shopCat['root_category_id'] = $item['root_cat'];
						$shopCat['shop_id'] = $shopId;
						$shopCat['created_by'] = $this->user['id'];
						$shopCat['created_at'] = date("Y-m-d H:i:s");
						$shopCat['updated_by'] = $this->user['id'];
						$shopCat['updated_at'] = date("Y-m-d H:i:s");
						
						array_push($new, $shopCat);
					}
				}
			}
			if(count($new)>0){
				
				if($this->ShopCategory->saveMany($new)){
					$this->redirect("/quan-ly-user");
				}
			}
			
		}
	}
	public function edit(){
		$this->layout = 'layoutNoMenuLeft';
		
		$this->loadModel('RootCategory');
		$this->loadModel('ShopCategory');
		$this->loadModel('Shop');
		
		$this->set('rootCategories',$this -> RootCategory->findAllData());
		$this->set('title_for_layout','Đăng ký danh mục');
		
		
		$conditions = array("Shop.created_by" => $this->user['id'], "Shop.deleted" =>0);
		$shop = $this->Shop->find('first', array('conditions' => $conditions));
		
		if(empty($shop)){
			$this->redirect("/quan-ly-user");
		}
		$shopId = $shop["Shop"]['id'];
		
		//chưa có shop category ko thể vào trang edit
		$shopCat = $this->ShopCategory->findShopCategoryByShopId($shopId, $this->user['id']);
		
		parent::createException($shopCat);
		
		$this->set('shopCat', $shopCat);
		$this->set("shopId",$shopId);
		
		if ($this -> request -> is('post')) {
			
			$data = $this -> request ->data;
			echo "<pre>";
			print_r($data);
			$new = array();
			
			foreach ($data as $item){
				if(isset($item['shopCat']) && count($item['shopCat'])>0){
					
					foreach ($item['shopCat'] as $i){
						$shopCat['category_id'] = $i;
						$shopCat['root_category_id'] = $item['root_cat'];
						$shopCat['shop_id'] = $shopId;//lấy thong tin user dang dang nhap
						$shopCat['created_by'] = $this->user['id'];
						$shopCat['created_at'] = date("Y-m-d H:i:s");
						$shopCat['updated_by'] = $this->user['id'];//lấy thong tin user dang dang nhap
						$shopCat['updated_at'] = date("Y-m-d H:i:s");
						
						array_push($new, $shopCat);
					}
				}
			}
			if(count($new)>0){
				$this-> ShopCategory->updateShopCategory($shopId, $new);
				$this->redirect("/quan-ly-user");
			}
			
		}
	}
}
?>